package com.neu.common.utils;

import net.sourceforge.pinyin4j.PinyinHelper;

import java.util.Locale;

public class ChineseUtils {
    /**
     * @Author Tang
     * 得到中文首字母（中国 -> ZG）
     * @param str 需要转化的中文字符串
     * @return 大写首字母缩写的字符串
     */
    public static String getFirstLetter(String str) {
        StringBuilder convert = new StringBuilder();
        char word = str.charAt(0);
        String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
        if (pinyinArray != null) {
            convert.append(pinyinArray[0].charAt(0));
        } else {
            convert.append(word);
        }
        return convert.toString().toUpperCase();
    }

    public static String getAllName(String str) {
        StringBuilder convert = new StringBuilder();
        for(int i=0;i<str.length();i++){
            char word = str.charAt(i);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                String sub = pinyinArray[0];
                convert.append(sub.substring(0,sub.length()-1));
            } else {
                convert.append(word);
            }
        }
        return convert.toString().toUpperCase();
    }
}
